package com.zlfund.cat.zjgsmk.ui.user;

import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.zlfund.cat.zjgsmk.R;
import com.zlfund.cat.zjgsmk.api.ZjgsmkService;
import com.zlfund.cat.zjgsmk.bean.UserBean;
import com.zlfund.cat.zjgsmk.constant.EventBusConstant;
import com.zlfund.cat.zjgsmk.constant.MessageEvent;
import com.zlfund.cat.zjgsmk.controller.HasLoginHelper;
import com.zlfund.cat.zjgsmk.net.INetCallback;
import com.zlfund.cat.zjgsmk.net.RetrofitQueryMapRequest;
import com.zlfund.cat.zjgsmk.ui.base.BaseActivity;
import com.zlfund.cat.zjgsmk.ui.base.ZApplication;
import com.zlfund.cat.zjgsmk.ui.home.MainActivity;
import com.zlfund.cat.zjgsmk.util.Logger;
import com.zlfund.cat.zjgsmk.util.SpUtils;
import com.zlfund.cat.zjgsmk.util.ToastUtil;
import com.zlfund.cat.zjgsmk.util.timetask.StringUtil;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;

public class LoginActivity extends BaseActivity {

    @BindView(R.id.iv_cancel)
    ImageView mIvCancel;
    @BindView(R.id.tv_login)
    TextView mTvLogin;
    @BindView(R.id.tv_register)
    TextView mTvRegister;
    @BindView(R.id.et_login_phone)
    EditText mEtLoginPhone;
    @BindView(R.id.et_login_pwd)
    EditText mEtLoginPwd;
    @BindView(R.id.tv_forget_pwd)
    TextView mTvForgetPwd;
    @BindView(R.id.btn_login)
    Button mBtnLogin;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    protected void setContentView() {
        setContentView(R.layout.activity_login);
    }

    @Override
    protected void setListener() {
        mBtnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String phone = mEtLoginPhone.getText().toString();
                String pwd = mEtLoginPwd.getText().toString();
                pwd = StringUtil.hexdigest(pwd);
                RetrofitQueryMapRequest<UserBean> request = new RetrofitQueryMapRequest<>();
                request.addParams("user", phone)
                        .addParams("pwd", pwd);
                request.build(ZjgsmkService.LoginService.class)
                        .enqueueWithRx("login", new INetCallback<UserBean>() {
                            @Override
                            public void response(UserBean userBean) {
                                UserBean.RBean rBean = userBean.getR();
                                Logger.i(rBean.getAuth());
                                if (rBean != null) {
                                    SpUtils.putStringToAccount(mContext, "card", rBean.getCard());
                                    SpUtils.putStringToAccount(mContext, "id", rBean.getId());
                                    SpUtils.putStringToAccount(mContext, "named", rBean.getNamed());
                                    SpUtils.putStringToAccount(mContext, "birthday", rBean.getBirthday());
                                    SpUtils.putStringToAccount(mContext, "sex", rBean.getSex());
                                    SpUtils.putStringToAccount(mContext, "name", rBean.getName());
                                    SpUtils.putStringToAccount(mContext, "ico", rBean.getIco());
                                    SpUtils.putStringToAccount(mContext, "avatar", rBean.getAvatar());
                                    SpUtils.putStringToAccount(mContext, "brief", rBean.getBrief());
                                    SpUtils.putStringToAccount(mContext, "auth", rBean.getAuth());
                                }
                                ZApplication.mContext.setLoginService(new HasLoginHelper());
                                EventBus.getDefault().post(new MessageEvent(EventBusConstant.MAINACTIVITYREFRESHDATA));
                                startActivity(new Intent(mContext, MainActivity.class));
                            }

                            @Override
                            public void error(String str) {
                                ToastUtil.show(str);
                            }
                        });
            }
        });
    }

    @Override
    protected void initData() {

    }
}
